/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.judge;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.util.Messages;

/**
 * @author Sukahda Tapshalkar
 * Aug 31, 2007
 *
 */
@SuppressWarnings("unchecked")
public class RejectConfirmationPage extends BaseBackingBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Properties
	 */
	private String rejectionReason;

	private UIInput rejectionReasonInput;

	private FacesMessage message; 
	

	
	/**
	 * Constructor
	 *
	 */
	public RejectConfirmationPage(){
		
	}

	/**
	 * Getter for property rejectionReason
	 * @return rejetionReason
	 */
	public String getRejectionReason() {
		return rejectionReason;
	}

	/**
	 * Setter for property rejectionReason
	 * @param rejectionReason
	 */
	public void setRejectionReason(String rejectionReason) {
		this.rejectionReason = rejectionReason;
	}

	/**
	 * Getter for property rejectionReasonInput
	 * @return rejectionReasonInput
	 */
	public UIInput getRejectionReasonInput() {
		return rejectionReasonInput;
	}

	/**
	 * Setter for property rejectionReasonInut
	 * @param rejectionReasonInput
	 */
	public void setRejectionReasonInput(UIInput rejectionReasonInput) {
		this.rejectionReasonInput = rejectionReasonInput;
	}
	
	
	/**
	 * This method resets the rejection reason input field
	 * @return String
	 */
	public String reset(){
		this.getRejectionReasonInput().setSubmittedValue(null);
		return "stay";
	}
	
	/**
	 * This mathod navigates user to the brevious page
	 * @return String
	 */
	public String back(){
		Map sessionMap = this.getFacesContext().getExternalContext()
		.getSessionMap();
		sessionMap.remove("judgeCaseDatailsPage");
		return "gotoCSECaseDetailsPage";
	}

	/**
	 * This method gets called when the judge confirm to reject
	 * the case record
	 * @return String
	 */
	public String rejectFiling() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		Person person = (Person)sessionMap.get("person");
		CaseDetailsPage caseDetailsPage = (CaseDetailsPage)sessionMap.get("judgeCaseDetailsPage");
		String caseUuid  = caseDetailsPage.getCaseRecord().getUuid();
		String courtUuid = (String) sessionMap.get("courtUuid");
		
		try {
			//@TODO add email notification
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		//update in the mysql database
		// CaseRecordStatus = rejected (3)
		this.getModelFacade().updateRejectedCaseRecord(caseUuid, this.getRejectionReason(),19, courtUuid);
		//CaseRecordEventType = rejected (3)
		this.getModelFacade().addCaseRecordHistory(caseUuid, 16, person, courtUuid,"no comments");
		//commit the transaction
		//HibernatePhaseListener.getSessionFactory().getCurrentSession()
		//.getTransaction().commit();		
		
		return "gotoChildEnforcementCaseTypePage";
	}

	/**
	 * This method validates the rejected reason typed by user
	 * @param context
	 * @param component
	 * @param value
	 * @throws ValidatorException
	 * @throws Exception
	 */
	public void validateReason(FacesContext context, UIComponent component,
			Object value) throws ValidatorException, Exception {
		String rejectionReason = (String) value;
		if (!isValidReason(rejectionReason)) {
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
	}

	/**
	 * Helper method to validate the rejection reason
	 * @param value
	 * @return
	 * @throws Exception
	 */
	protected boolean isValidReason(String value) throws Exception {
		boolean isValid = true;
		char[] charArray = value.toCharArray();
		for (char c : charArray) {
			if (Character.isDigit(c) || Character.isLetter(c)
					|| Character.isSpaceChar(c)) {
			} else {
				String s = new Character(c).toString();
				if (s.equals("!") || s.equals(",") || s.equals(";")
						|| s.equals(".") || s.equals("'") || s.equals("\"")
						|| s.equals("%")) {
				} else {
					isValid = false;
					message = Messages.getMessage(
							"us.gaaoc.framework.webapp.util.errorMessage",
							"incorrectRejectionReason", new Object[] { value, s });
					message.setDetail(message.getDetail());
					message.setSeverity(FacesMessage.SEVERITY_ERROR);
					break;
				}
			}
		}
		return isValid;
	}
}
